package com.wondersgroup.insurance.dnuilearn.views.shader;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ComposeShader;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * Created by Administrator on 2017/5/12.
 */

public class RadarView1 extends View {
    //雷达扫描时候的起始和终止颜色
    private int mStartColor = 0x0000ff00;

    private int mEndColor = 0xaa00ff00;
    int radius = 0;
    int centerX = 0;
    int centerY = 0;

    final int CIRCLENUMBER = 4;
    Paint paint = null;

    private int[] mColors = {mStartColor,mEndColor};


    BitmapShader bmpShader;


    int start = 0;
    SweepGradient mSweepGradient = null;


    boolean isStart = true;
    public RadarView1(Context context) {
        this(context, null);
    }

    public RadarView1(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RadarView1(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paint = new Paint();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        centerX = w/2;
        centerY = h/2;
        radius = Math.min(centerX,centerY) - 30;

        getBackGroudShader();
        getSweepShader();

        Log.e("","onSizeChanged");
    }

//
//    1.PorterDuff.Mode.CLEAR
//    所绘制不会提交到画布上。
//            2.PorterDuff.Mode.SRC
//            显示上层绘制图片
//3.PorterDuff.Mode.DST
//            显示下层绘制图片
//4.PorterDuff.Mode.SRC_OVER
//    正常绘制显示，上下层绘制叠盖。
//            5.PorterDuff.Mode.DST_OVER
//    上下层都显示。下层居上显示。
//            6.PorterDuff.Mode.SRC_IN
//    取两层绘制交集。显示上层。
//            7.PorterDuff.Mode.DST_IN
//    取两层绘制交集。显示下层。
//            8.PorterDuff.Mode.SRC_OUT
//    取上层绘制非交集部分。
//            9.PorterDuff.Mode.DST_OUT
//    取下层绘制非交集部分。
//            10.PorterDuff.Mode.SRC_ATOP
//            取下层非交集部分与上层交集部分
//11.PorterDuff.Mode.DST_ATOP
//            取上层非交集部分与下层交集部分
//12.PorterDuff.Mode.XOR
//    异或：去除两图层交集部分
//13.PorterDuff.Mode.DARKEN
//    取两图层全部区域，交集部分颜色加深
//14.PorterDuff.Mode.LIGHTEN
//    取两图层全部，点亮交集部分颜色
//15.PorterDuff.Mode.MULTIPLY
//            取两图层交集部分叠加后颜色
//16.PorterDuff.Mode.SCREEN
//    取两图层全部区域，交集部分变为透明色

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawBackGoundCircle(canvas);
        drawDividerCircle(canvas);

//        if (mSweepGradient ==null){
            mSweepGradient = new SweepGradient(radius, radius, mColors, null);
//        }
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.FILL);
        Matrix mMatrix = new Matrix();
        mMatrix.postRotate(30,radius,radius);
        mSweepGradient.setLocalMatrix(mMatrix);
        paint.setShader(mSweepGradient);
        canvas.drawCircle(radius,radius,radius,paint);

//        paint.reset();
//            Matrix matrix = new Matrix();
//            matrix.postRotate(++start > 360?  start =0 :start,centerX,centerY);
//            mSweepGradient.setLocalMatrix(matrix);
//
//        mComposeShader = new ComposeShader(bmpShader, mSweepGradient, PorterDuff.Mode.MULTIPLY);
//        paint.setShader(mComposeShader);
//        canvas.drawCircle(centerX,centerY,radius,paint);
//        if (isStart){
//            postInvalidateDelayed(20);
//        }

    }


    public  void start(){
        if (!isStart){
            isStart = true;
            invalidate();
        }

    }

    public  void stop(){
        isStart = false;

    }



    ComposeShader mComposeShader = null;

    void getBackGroudShader(){
        if (bmpShader == null){
            Bitmap  bmp = Bitmap.createBitmap(radius*2,radius*2, Bitmap.Config.ARGB_8888);
            Canvas bmpCanvas = new Canvas(bmp);
            drawBackGoundCircle(bmpCanvas);
            drawDividerCircle(bmpCanvas);

            bmpShader = new BitmapShader(bmp, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
            Matrix matrix = new Matrix();
            matrix.setTranslate(centerX-radius,centerY-radius);
            bmpShader.setLocalMatrix(matrix);
        }

    }


    void getSweepShader(){
        if (mSweepGradient ==null){
            mSweepGradient = new SweepGradient(centerX, centerY, mColors, new float[]{0.25f,1.0f});
        }

    }



    void drawBackGoundCircle(Canvas canvas){
        paint.reset();
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.FILL);
//        canvas.drawCircle(centerX,centerY,radius,paint);
        canvas.drawCircle(radius,radius,radius,paint);
    }

    void drawDividerCircle(Canvas canvas){
        paint.reset();
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(2);
        int delty = radius /CIRCLENUMBER;
//        for (int i=1; i <CIRCLENUMBER; i++){
//            canvas.drawCircle(centerX,centerY,delty*i,paint);
//        }
//        Path path = new Path();
//        // x
//        path.moveTo(centerX-radius,centerY);
//        path.lineTo(centerX+radius,centerY);
//        //y
//        path.moveTo(centerX,centerY-radius);
//        path.lineTo(centerX,centerY+radius);
//        canvas.drawPath(path,paint);



        for (int i=1; i <CIRCLENUMBER; i++){
            canvas.drawCircle(radius,radius,delty*i,paint);
        }
        Path path = new Path();
        // x
        path.moveTo(0,radius);
        path.lineTo(radius*2,radius);
        //y
        path.moveTo(radius,0);
        path.lineTo(radius,radius*2);
        canvas.drawPath(path,paint);

    }

}
